﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace LivroBebe.Data
{
    /// <summary>
    /// Classe Genérica responsável pela Manipulação de Dados (Inserção, Leitura, Atualização e Exclusão) 
    /// </summary>
    public class DBComum
    {
        #region Métodos
        public static Boolean ExecuteNonQuery(IEnumerable<DBParametros> dbParametros, string storedprocedure)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetStoredProcCommand(storedprocedure);
                foreach (DBParametros parametro in dbParametros)
                    db.AddInParameter(dbCommand, parametro.Parametro, parametro.Tipo, parametro.Valor);
                db.ExecuteNonQuery(dbCommand);

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static IDataReader ExecuteReader(IEnumerable<DBParametros> dbParametros, string storedprocedure)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetStoredProcCommand(storedprocedure);
                foreach (DBParametros parametro in dbParametros)
                    db.AddInParameter(dbCommand, parametro.Parametro, parametro.Tipo, parametro.Valor);
                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static int ExecuteScalar(IEnumerable<DBParametros> dbParametros, string storedprocedure)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetStoredProcCommand(storedprocedure);
                foreach (DBParametros parametro in dbParametros)
                    db.AddInParameter(dbCommand, parametro.Parametro, parametro.Tipo, parametro.Valor);
                return (int)db.ExecuteScalar(dbCommand);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DataSet ExecuteDataSet(IEnumerable<DBParametros> dbParametros, string storedprocedure)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetStoredProcCommand(storedprocedure);
                foreach (DBParametros parametro in dbParametros)
                    db.AddInParameter(dbCommand, parametro.Parametro, parametro.Tipo, parametro.Valor);
                return db.ExecuteDataSet(dbCommand);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}